package com.agile.leetcode.jsonpath;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/9/5 11:55
 */
public class JsonPathTest {
    public static void main(String[] args) {
        Store store = new Store();
        Book book1 = new Book();
        book1.setAuthor("czk");
        book1.setCategory("蜀国");
        book1.setTitle("三国");
        book1.setPrice(10.0);
        Book book2 = new Book();
        book2.setAuthor("czj");
        book2.setCategory("林黛玉");
        book2.setTitle("红楼梦");
        book2.setPrice(14.5);
        Book book3 = new Book();
        book3.setAuthor("nzq");
        book3.setCategory("孙悟空");
        book3.setTitle("西游记");
        book1.setPrice(15.7);
        List<Book> book = new ArrayList<>();
        book.add(book1);
        book.add(book2);
        book.add(book3);
        store.setBook(book);
        Bicycle bicycle=new Bicycle();
        bicycle.setPrice(14.22);
        bicycle.setColor("red");
        store.setBicycle(bicycle);
        Out out=new Out();
        out.setStore(store);
        out.setExpensive(14.66);
        // 转为json
        String jsonString = JSONObject.toJSONString(out);
        System.out.println(jsonString);

        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        // 获取json中store下book下第一个author值
        Object eval = JSONPath.eval(jsonObject, "$.store.book[0].author");
        System.out.println(eval);
        // 获取json中store下book下第二个price值
        System.out.println(JSONPath.eval(jsonObject,"$.store.book[1].price"));
        // 获取所有json中所有author的值
        System.out.println(JSONPath.eval(jsonObject,"$..author"));
        // 所有的东西，书籍和自行车
        System.out.println(JSONPath.eval(jsonObject,"$.store.*"));
        // 获取json中store下所有price的值
        System.out.println(JSONPath.eval(jsonObject,"$.store..price"));
    }
}
